package com.ruoyi.function.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.function.domain.Garage;
import com.ruoyi.function.domain.Technician;
import com.ruoyi.function.mapper.GarageMapper;
import com.ruoyi.function.mapper.TechnicianMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.function.mapper.UserFlowingMapper;
import com.ruoyi.function.domain.UserFlowing;
import com.ruoyi.function.service.IUserFlowingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户积分流水Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@Service
public class UserFlowingServiceImpl implements IUserFlowingService {
    @Autowired
    private UserFlowingMapper userFlowingMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private TechnicianMapper technicianMapper;
    @Autowired
    private GarageMapper garageMapper;

    /**
     * 查询用户积分流水
     *
     * @param userFlowingId 用户积分流水主键
     * @return 用户积分流水
     */
    @Override
    public UserFlowing selectUserFlowingByUserFlowingId(Long userFlowingId) {
        return userFlowingMapper.selectUserFlowingByUserFlowingId(userFlowingId);
    }

    /**
     * 查询用户积分流水列表
     *
     * @param userFlowing 用户积分流水
     * @return 用户积分流水
     */
    @Override
    public List<UserFlowing> selectUserFlowingList(UserFlowing userFlowing) {
        return userFlowingMapper.selectUserFlowingList(userFlowing);
    }

    /**
     * 新增用户积分流水
     *
     * @param userFlowing 用户积分流水
     * @return 结果
     */
    @Override
    public int insertUserFlowing(UserFlowing userFlowing) {
        userFlowing.setCreateTime(DateUtils.getNowDate());
        return userFlowingMapper.insertUserFlowing(userFlowing);
    }

    /**
     * 修改用户积分流水
     *
     * @param userFlowing 用户积分流水
     * @return 结果
     */
    @Override
    public int updateUserFlowing(UserFlowing userFlowing) {
        userFlowing.setUpdateTime(DateUtils.getNowDate());
        return userFlowingMapper.updateUserFlowing(userFlowing);
    }

    /**
     * 批量删除用户积分流水
     *
     * @param userFlowingIds 需要删除的用户积分流水主键
     * @return 结果
     */
    @Override
    public int deleteUserFlowingByUserFlowingIds(Long[] userFlowingIds) {
        return userFlowingMapper.deleteUserFlowingByUserFlowingIds(userFlowingIds);
    }

    /**
     * 删除用户积分流水信息
     *
     * @param userFlowingId 用户积分流水主键
     * @return 结果
     */
    @Override
    public int deleteUserFlowingByUserFlowingId(Long userFlowingId) {
        return userFlowingMapper.deleteUserFlowingByUserFlowingId(userFlowingId);
    }

    /**
     * 添加积分
     *
     * @param userId 用户id
     * @param balance 积分
     * @param type 类型
     * @param userTypes 用户类型
     * @param subordinateId
     */
    @Override
    @Transactional
    public void addAmount(Long userId, BigDecimal balance, Integer type, String userTypes, Long subordinateId) {
        balance = balance.setScale(2, BigDecimal.ROUND_HALF_UP);
        SysUser user = userService.selectUserById(userId);
        BigDecimal balance1 = null;
        if ("2".equals(userTypes)) {
            // 查询技师
            Technician technician = technicianMapper.selectByUserId(userId);
            balance1= technician.getBalance().add(balance);
            if (technician.getBalance().add(balance).compareTo(BigDecimal.ZERO) < 0) {
                throw new ServiceException("余额不足");
            }

            // 通过用户id 修改余额
            int i = technicianMapper.upbatchById(balance, technician.getId());
            if (i<=0){
                throw new ServiceException("提现失败");
            }
        } else if ("3".equals(userTypes)) {
            // 查询技师
            Garage garage = garageMapper.selectByUserId(userId);
            balance1= garage.getBalance().add(balance);
            if (garage.getBalance().add(balance).compareTo(BigDecimal.ZERO) < 0) {
                throw new ServiceException("余额不足");
            }
            // 通过用户id 修改余额
            int i = garageMapper.upbatchById(balance, garage.getId());
            if (i<=0){
                throw new ServiceException("提现失败");
            }
        }else {
            throw new ServiceException("用户类型错误");
        }

        //增加积分记录
        UserFlowing userFlowing = new UserFlowing();
        userFlowing.setAmount(balance);
        userFlowing.setType(type);
        userFlowing.setSubordinateId(subordinateId);
        userFlowing.setCreateBy(SecurityUtils.getUserId() + "");
        userFlowing.setCreateTime(DateUtils.getNowDate());
        userFlowing.setBalance(balance1);
        userFlowingMapper.insertUserFlowing(userFlowing);
    }
}
